<Project>
  <PropertyGroup>
    <BuildHelixWorkItemsDependsOn>$(BuildHelixWorkItemsDependsOn);PrepareForBuildHelixWorkItems_Mobile</BuildHelixWorkItemsDependsOn>
    <EnableDefaultBuildHelixWorkItems>false</EnableDefaultBuildHelixWorkItems>
    <IncludeHelixCorrelationPayload>false</IncludeHelixCorrelationPayload>

    <NeedsToBuildAppsOnHelix Condition="('$(TargetOS)' == 'ios' or '$(TargetOS)' == 'iossimulator' or '$(TargetOS)' == 'tvos' or '$(TargetOS)' == 'tvossimulator') and '$(NeedsToBuildAppsOnHelix)' == '' and '$(Scenario)' == 'BuildiOSApps'">true</NeedsToBuildAppsOnHelix>
    <NeedsiOSSDK Condition="'$(NeedsToBuildAppsOnHelix)' == 'true' and '$(NeedsWorkload)' != 'true'">true</NeedsiOSSDK>
  </PropertyGroup>

  <ItemDefinitionGroup Condition="'$(TargetOS)' == 'maccatalyst'">
    <XHarnessAppBundleToTest>
      <Targets>maccatalyst</Targets>
      <TestTimeout>$(_workItemTimeout)</TestTimeout>
      <LaunchTimeout>$(_workItemTimeout)</LaunchTimeout>
    </XHarnessAppBundleToTest>
  </ItemDefinitionGroup>

  <ItemDefinitionGroup Condition="'$(TargetOS)' == 'android'">
    <XHarnessApkToTest>
        <Targets Condition="'$(TargetArchitecture)' == 'arm'">armeabi-v7a</Targets>
        <Targets Condition="'$(TargetArchitecture)' == 'arm64'">arm64-v8a</Targets>
        <Targets Condition="'$(TargetArchitecture)' == 'x64'">x86_64</Targets>
        <Targets Condition="'$(TargetArchitecture)' == 'x86'">x86</Targets>
        <AndroidInstrumentationName>net.dot.MonoRunner</AndroidInstrumentationName>
        <TestTimeout>$(_workItemTimeout)</TestTimeout>
    </XHarnessApkToTest>
  </ItemDefinitionGroup>

  <PropertyGroup Condition="'$(NeedsiOSSDK)' == 'true'">
    <NeedsDotNetSdk>true</NeedsDotNetSdk>
    <UseDotNetCliVersionFromGlobalJson>true</UseDotNetCliVersionFromGlobalJson>
    <IncludeXHarnessCli>true</IncludeXHarnessCli>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetsAppleMobile)' == 'true'">
    <HelixPreCommand Include="export XHARNESS_DISABLE_COLORED_OUTPUT=true" />
    <HelixPreCommand Include="export XHARNESS_LOG_WITH_TIMESTAMPS=true" />
  </ItemGroup>

  <Target Name="PrepareForBuildHelixWorkItems_Mobile">

    <PropertyGroup Condition="'$(TargetOS)' == 'ios' or '$(TargetOS)' == 'tvos' or '$(TargetOS)' == 'iossimulator' or '$(TargetOS)' == 'tvossimulator' or '$(TargetOS)' == 'maccatalyst'">
      <AppleTestTarget Condition="'$(TargetOS)' == 'iossimulator'">ios-simulator-64</AppleTestTarget>
      <AppleTestTarget Condition="'$(TargetOS)' == 'tvossimulator'">tvos-simulator</AppleTestTarget>
      <AppleTestTarget Condition="'$(TargetOS)' == 'ios'">ios-device</AppleTestTarget>
      <AppleTestTarget Condition="'$(TargetOS)' == 'tvos'">tvos-device</AppleTestTarget>
      <AppleTestTarget Condition="'$(TargetOS)' == 'maccatalyst'">maccatalyst</AppleTestTarget>
    </PropertyGroup>

    <PropertyGroup Condition="'$(TargetsAppleMobile)' == 'true'">
      <iOSLikeBuildTargetsDir>$([MSBuild]::NormalizeDirectory('$(RepoRoot)', 'src', 'mono', 'msbuild', 'apple', 'build'))</iOSLikeBuildTargetsDir>
      <iOSLikeLibraryBuilderTargetsDir>$([MSBuild]::NormalizeDirectory('$(RepoRoot)', 'src', 'mono', 'msbuild', 'common'))</iOSLikeLibraryBuilderTargetsDir>
      <WorkItemPrefix Condition="'$(Scenario)' == 'BuildiOSApps' and '$(TestUsingWorkloads)' != 'true'">$(TargetOS)-</WorkItemPrefix>

      <CMakeUrl>https://netcorenativeassets.blob.core.windows.net/resource-packages/external/macos/cmake/cmake-3.28.0-macos-universal.tar.gz</CMakeUrl>

      <_XHarnessAppleCustomCommand Condition="'$(NeedsiOSSDK)' == 'true'">
        source build-apple-app.sh
      </_XHarnessAppleCustomCommand>
      <_RuntimeComponentManifestDir>$([MSBuild]::NormalizeDirectory('$(MonoArtifactsPath)', 'build'))</_RuntimeComponentManifestDir>
    </PropertyGroup>

    <ItemGroup Condition="'$(NeedsiOSSDK)' == 'true'">
      <HelixCorrelationPayload Include="cmake" Uri="$(CMakeUrl)"                 Destination="build/cmake" />
      <HelixCorrelationPayload Include="$(AppleAppBuilderDir)"                   Destination="build/AppleAppBuilder" />
      <HelixCorrelationPayload Include="$(LibraryBuilderDir)"                    Destination="build/LibraryBuilder" />
      <HelixCorrelationPayload Include="$(MonoAOTCompilerDir)"                   Condition="'$(RuntimeFlavor)' == 'mono'"
                                                                                 Destination="build/MonoAOTCompiler" />
      <HelixCorrelationPayload Include="$(MicrosoftNetCoreAppRuntimePackDir)"    Destination="build/microsoft.netcore.app.runtime.$(TargetOS)-$(TargetArchitecture.ToLower())" />
      <HelixCorrelationPayload Include="$(_RuntimeComponentManifestDir)"         Condition="'$(RuntimeFlavor)' == 'mono'"
                                                                                 Destination="build/microsoft.netcore.app.runtime.$(TargetOS)-$(TargetArchitecture.ToLower())/runtimes/$(TargetOS)-$(TargetArchitecture.ToLower())/build" />
      <HelixCorrelationPayload Include="$(iOSLikeBuildTargetsDir)"               Destination="build/apple" />
      <HelixCorrelationPayload Include="$(iOSLikeLibraryBuilderTargetsDir)"      Destination="build/common" />
      <HelixCorrelationPayload Include="$(MonoAotCrossDir)"                      Condition="'$(RuntimeFlavor)' == 'mono'"
                                                                                 Destination="build/cross" />
      <HelixCorrelationPayload Include="$(MonoTargetsTasksDir)"                  Destination="build/MonoTargetsTasks" />
      <HelixCorrelationPayload Include="$(CoreCLRCrossILCompilerDir)"            Condition="'$(RuntimeFlavor)' == 'coreclr'"
                                                                                 Destination="build/ilc" />
      <HelixCorrelationPayload Include="$(CoreCLRBuildIntegrationDir)"           Condition="'$(RuntimeFlavor)' == 'coreclr'"
                                                                                 Destination="build/BuildIntegration" />
      <HelixCorrelationPayload Include="$(CoreCLRAotSdkDir)"                     Condition="'$(RuntimeFlavor)' == 'coreclr'"
                                                                                 Destination="build/aotsdk" />
      <HelixCorrelationPayload Include="$(ToolsILLinkDir)"                       Destination="build/microsoft.net.illink.tasks" />
    </ItemGroup>

    <ItemGroup Condition="'$(TargetsAppleMobile)' == 'true'">
      <!-- Create work items for test apps -->
      <XHarnessAppBundleToTest
        Include="$([System.IO.Directory]::GetDirectories('$(TestArchiveTestsRoot)', '*.app', System.IO.SearchOption.AllDirectories))">
        <TestTarget>$(AppleTestTarget)</TestTarget>
        <TestTimeout>$(_workItemTimeout)</TestTimeout>
        <CustomCommands>$(_XHarnessAppleCustomCommand)</CustomCommands>
      </XHarnessAppBundleToTest>

      <!-- To save on overall size, we compress each app after building. -->
      <XHarnessAppBundleToTest 
        Include="$([System.IO.Directory]::GetFiles('$(TestArchiveTestsRoot)$(OSPlatformConfig)', '*.zip', System.IO.SearchOption.TopDirectoryOnly))"
        Exclude="$([System.IO.Directory]::GetFiles('$(TestArchiveRoot)', 'xharness-app-payload*', System.IO.SearchOption.AllDirectories))">
        <TestTarget>$(AppleTestTarget)</TestTarget>
        <TestTimeout>$(_workItemTimeout)</TestTimeout>
        <CustomCommands>$(_XHarnessAppleCustomCommand)</CustomCommands>
      </XHarnessAppBundleToTest>

      <!--
        Create work items for run-only apps

        Note: We're excluding iOS and tvOS device runonly as mlaunch does not seem to return and times out.
      -->
      <XHarnessAppBundleToTest Condition="Exists('$(TestArchiveRoot)runonly') and '$(TargetOS)' != 'tvos' and '$(TargetOS)' != 'ios'" Include="$([System.IO.Directory]::GetDirectories('$(TestArchiveRoot)runonly', '*.app', System.IO.SearchOption.AllDirectories))" >
        <!-- The sample app doesn't need test runner -->
        <IncludesTestRunner>false</IncludesTestRunner>
        <!-- The sample's C# Main method returns 42 so it should be considered by xharness as a success -->
        <ExpectedExitCode>42</ExpectedExitCode>
        <TestTarget>$(AppleTestTarget)</TestTarget>
      </XHarnessAppBundleToTest>

      <!-- To save on overall size, we compress each app after building. -->
      <XHarnessAppBundleToTest Condition="Exists('$(TestArchiveRoot)runonly/$(OSPlatformConfig)') and '$(TargetOS)' != 'tvos' and '$(TargetOS)' != 'ios'" 
        Include="$([System.IO.Directory]::GetFiles('$(TestArchiveRoot)runonly/$(OSPlatformConfig)', '*.zip', System.IO.SearchOption.TopDirectoryOnly))" 
        Exclude="$([System.IO.Directory]::GetFiles('$(TestArchiveRoot)runonly', 'xharness-app-payload*', System.IO.SearchOption.AllDirectories))">
        <!-- The sample app doesn't need test runner -->
        <IncludesTestRunner>false</IncludesTestRunner>
        <!-- The sample's C# Main method returns 42 so it should be considered by xharness as a success -->
        <ExpectedExitCode>42</ExpectedExitCode>
        <TestTarget>$(AppleTestTarget)</TestTarget>
      </XHarnessAppBundleToTest>
    </ItemGroup>

    <ItemGroup Condition="'$(TargetOS)' == 'android'">
      <_apks Include="$(TestArchiveTestsRoot)**/*.apk" />
      <XHarnessApkToTest Include="@(_apks)">
        <AndroidPackageName>net.dot.%(Filename)</AndroidPackageName>
      </XHarnessApkToTest>
      <_runonlyApks Include="$(TestArchiveRoot)runonly/**/*.apk" />
      <XHarnessApkToTest Include="@(_runonlyApks)">
        <AndroidPackageName>net.dot.%(Filename)</AndroidPackageName>
        <!-- The android sample returns 42 so it should be considered by xharness as a success -->
        <ExpectedExitCode>42</ExpectedExitCode>
      </XHarnessApkToTest>
    </ItemGroup>
  </Target>

  <Target Name="AfterBuildHelixWorkItems_Mobile" AfterTargets="BuildHelixWorkItems">

    <PropertyGroup>
      <_TestPath Condition="'%(XHarnessAppBundleToTest.CustomCommands)' != ''">$([System.IO.Path]::GetDirectoryName('%(XHarnessAppBundleToTest.Identity)'))</_TestPath>
    </PropertyGroup>

    <Exec Condition="'$(TargetOS)' == 'ios' or '$(TargetOS)' == 'tvos'"
          Command="du -sh &quot;$(_TestPath)&quot;"
          ContinueOnError="true"
          IgnoreExitCode="true"
          IgnoreStandardErrorWarningFormat="true" />

  </Target>
</Project>
